﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AuctionHouseDataLibrary;
using System.Runtime.Serialization;

namespace ItemDatabase
{
    [Serializable]
    [DataContract]
    public class PagedData<T>
    {
        [DataMember]
        public int total { get; set; }
        [DataMember]
        public List<T> Items { get; set; }
        [DataMember]
        public bool success { get; set; }
        [DataMember]
        public string message { get; set; }

        public PagedData(IQueryable<T> query, int page, int start, int limit, string sort, string dir, string defaultSortColumn)
        {
            try
            {
                total = query.Count();
                success = false;
                string sortColumn = defaultSortColumn;
                string orderAscDesc = "ASC";
                if (!string.IsNullOrEmpty(sort))
                {
                    if (!string.IsNullOrEmpty(dir))
                    {
                        if (dir == "DESC")
                        {
                            orderAscDesc = "DESC";
                        }
                    }
                    sortColumn = sort;
                }
                Items = query.OrderBy(sortColumn + " " + orderAscDesc).Skip(start).Take(limit).ToList();
                success = true;
                message = "OK";
            }
            catch (Exception e)
            {
                total = 0;
                success = false;
                message = "Unhandled exception: " + e.Message;
                Items = new List<T>();
                throw;
            }
        }
    
    }
}